using System.Xml.Linq;
using Engine;

namespace Game
{
	public class DispenserWidget : CanvasWidget
	{
		private SubsystemTerrain m_subsystemTerrain;

		private ComponentDispenser m_componentDispenser;

		private ComponentBlockEntity m_componentBlockEntity;

		private GridPanelWidget m_inventoryGrid;

		private GridPanelWidget m_dispenserGrid;

		private ButtonWidget m_dispenseButton;

		private ButtonWidget m_shootButton;

		private CheckboxWidget m_acceptsDropsBox;

		public DispenserWidget(IInventory inventory, ComponentDispenser componentDispenser)
		{
			m_componentDispenser = componentDispenser;
			m_componentBlockEntity = componentDispenser.Entity.FindComponent<ComponentBlockEntity>(throwOnError: true);
			m_subsystemTerrain = componentDispenser.Project.FindSubsystem<SubsystemTerrain>(throwOnError: true);
			XElement node = ContentManager.Get<XElement>("Widgets/DispenserWidget");
			LoadContents(this, node);
			m_inventoryGrid = Children.Find<GridPanelWidget>("InventoryGrid");
			m_dispenserGrid = Children.Find<GridPanelWidget>("DispenserGrid");
			m_dispenseButton = Children.Find<ButtonWidget>("DispenseButton");
			m_shootButton = Children.Find<ButtonWidget>("ShootButton");
			m_acceptsDropsBox = Children.Find<CheckboxWidget>("AcceptsDropsBox");
			int num = 0;
			for (int i = 0; i < m_dispenserGrid.RowsCount; i++)
			{
				for (int j = 0; j < m_dispenserGrid.ColumnsCount; j++)
				{
					InventorySlotWidget inventorySlotWidget = new InventorySlotWidget();
					inventorySlotWidget.AssignInventorySlot(componentDispenser, num++);
					m_dispenserGrid.Children.Add(inventorySlotWidget);
					m_dispenserGrid.SetWidgetCell(inventorySlotWidget, new Point2(j, i));
				}
			}
			num = 10;
			for (int k = 0; k < m_inventoryGrid.RowsCount; k++)
			{
				for (int l = 0; l < m_inventoryGrid.ColumnsCount; l++)
				{
					InventorySlotWidget inventorySlotWidget2 = new InventorySlotWidget();
					inventorySlotWidget2.AssignInventorySlot(inventory, num++);
					m_inventoryGrid.Children.Add(inventorySlotWidget2);
					m_inventoryGrid.SetWidgetCell(inventorySlotWidget2, new Point2(l, k));
				}
			}
		}

		public override void Update()
		{
			int value = m_subsystemTerrain.Terrain.GetCellValue(m_componentBlockEntity.Coordinates.X, m_componentBlockEntity.Coordinates.Y, m_componentBlockEntity.Coordinates.Z);
			int data = Terrain.ExtractData(value);
			if (m_dispenseButton.IsClicked)
			{
				data = DispenserBlock.SetMode(data, DispenserBlock.Mode.Dispense);
				value = Terrain.ReplaceData(value, data);
				m_subsystemTerrain.ChangeCell(m_componentBlockEntity.Coordinates.X, m_componentBlockEntity.Coordinates.Y, m_componentBlockEntity.Coordinates.Z, value);
			}
			if (m_shootButton.IsClicked)
			{
				data = DispenserBlock.SetMode(data, DispenserBlock.Mode.Shoot);
				value = Terrain.ReplaceData(value, data);
				m_subsystemTerrain.ChangeCell(m_componentBlockEntity.Coordinates.X, m_componentBlockEntity.Coordinates.Y, m_componentBlockEntity.Coordinates.Z, value);
			}
			if (m_acceptsDropsBox.IsClicked)
			{
				data = DispenserBlock.SetAcceptsDrops(data, !DispenserBlock.GetAcceptsDrops(data));
				value = Terrain.ReplaceData(value, data);
				m_subsystemTerrain.ChangeCell(m_componentBlockEntity.Coordinates.X, m_componentBlockEntity.Coordinates.Y, m_componentBlockEntity.Coordinates.Z, value);
			}
			DispenserBlock.Mode mode = DispenserBlock.GetMode(data);
			m_dispenseButton.IsChecked = mode == DispenserBlock.Mode.Dispense;
			m_shootButton.IsChecked = mode == DispenserBlock.Mode.Shoot;
			m_acceptsDropsBox.IsChecked = DispenserBlock.GetAcceptsDrops(data);
			if (!m_componentDispenser.IsAddedToProject)
			{
				base.ParentWidget.Children.Remove(this);
			}
		}
	}
}
